/**ai出牌 */
var PkCardTool = require('../../FindTool/PkCardTool');
var _ = require('../../../FrameWork/we_underscore');
var FindSameCardValueTool = require('../../FindTool/FindSameCardValueTool');
var EumPkGroup = require('../../Enum/EumPkGroup');
var AIGetThree = module.exports = {};

AIGetThree.doMore = function(_lastCardInfo, _cardsInfo) {
    var resultMore = {
        isFind: false,
        allGetCardsGroup: []
    }
    for (var i = 3; i <= 4; ++i) {
        var tmpResult = this.do(_lastCardInfo, _cardsInfo, i);
        if (tmpResult.isFind) {
            _.each(tmpResult.allGetCardsGroup, function(_cardsGroup) {
                var cnt = _.size(_cardsGroup);
                if (cnt == 3) {
                    resultMore.allGetCardsGroup.push(_cardsGroup);
                } else {
                    resultMore.allGetCardsGroup.push([_cardsGroup[0], _cardsGroup[1], _cardsGroup[2]]);
                }
            })
        }
    }
    resultMore.isFind = _.size(resultMore.allGetCardsGroup) > 0;
    return resultMore;
};

/***AI出牌
 * 三张牌
 * _cardsInfo:所有手牌
 */
AIGetThree.do = function(_lastCardInfo, _cardsInfo, _number) {
    _number = null == _number ? 3 : _number;
    var cardCnt = _.size(_cardsInfo);
    if (cardCnt < 3) {
        return {
            isFind: false
        };
    }
    /**需要要起得牌 */
    var _lastThreeSame = FindSameCardValueTool.check(_lastCardInfo.cardsInfo);
    _lastThreeSame = _.filter(_lastThreeSame, function(cardList) {
        return 3 == _.size(cardList);
    });

    if (_.size(_lastThreeSame) == 0) {
        return {
            isFind: false
        };
    }
    _lastThreeSame = _.flatten(_lastThreeSame);
    var needGetCardInfo = _lastThreeSame[0];
    var needGetValue = needGetCardInfo.value;

    var canGetCardsInfo = _.filter(_cardsInfo, function(_cardInfo) {
        return _cardInfo.value > needGetValue
    });

    var sameCardsList = FindSameCardValueTool.check(canGetCardsInfo);

    var singleList = _.filter(sameCardsList, function(cardList) {
        return _.size(cardList) == _number;
    });

    if (_.size(singleList) < 1) {
        return {
            isFind: false
        };
    }
    if (_.size(singleList) > 0) {
        return {
            isFind: true,
            type: EumPkGroup.THREE,
            recommendCards: _.clone(singleList[0]), //推荐组
            allGetCardsGroup: singleList //所有组 
        };
    }
    return {
        isFind: false
    };
};

/***AI出牌
 * 三张带1
 */
AIGetThree.doBeltOneMore = function(_lastCardInfo, _cardsInfo) {
    var resultMore = {
        isFind: false,
        allGetCardsGroup: []
    };

    for (var i = 3; i <= 4; ++i) {
        var result = this.doBeltOne(_lastCardInfo, _cardsInfo, i);
        if (result.isFind) {
            _.each(result.allGetCardsGroup, function(_cardsInfoTmp) {
                var pushCard = _.clone(_cardsInfoTmp);
                if (_.size(_cardsInfoTmp) > 3) {
                    pushCard = [_cardsInfoTmp[0], _cardsInfoTmp[1], _cardsInfoTmp[2]];
                }
                var otherCards = _.difference(_cardsInfo, pushCard);
                var beltCard = this.getBeltCard(otherCards);
                pushCard.push(beltCard);
                resultMore.allGetCardsGroup.push(pushCard);
            }.bind(this));
        }
    }
    resultMore.isFind = _.size(resultMore.allGetCardsGroup) > 0;
    return resultMore;
};

/***AI出牌
 * 三张带1
 */
AIGetThree.doBeltOne = function(_lastCardInfo, _cardsInfo, _number) {
    var cardCnt = _.size(_cardsInfo);
    if (cardCnt < 4) {
        return {
            isFind: false
        };
    }
    var result = this.do(_lastCardInfo, _cardsInfo, _number);
    /***找到可能三张*/
    if (result.isFind) {
        var recommendCards = result.recommendCards; ////推荐组这里是只有三张一样的牌
        var otherCards = _.difference(_cardsInfo, recommendCards);
        var beltCard = this.getBeltCard(otherCards);
        recommendCards.push(beltCard);
        //计算带的那张牌
        return {
            isFind: true,
            type: EumPkGroup.THREEONE,
            recommendCards: recommendCards, //推荐组
            allGetCardsGroup: result.allGetCardsGroup //所有组 
        };
    }
    return {
        isFind: false
    };
};

/***AI出牌 
 */
AIGetThree.doBeltPairsMore = function(_lastCardInfo, _cardsInfo) {
    var resultMore = {
        isFind: false,
        allGetCardsGroup: []
    };

    for (var i = 3; i <= 4; ++i) {
        var result = this.doBeltOne(_lastCardInfo, _cardsInfo, i);
        if (result.isFind) {
            _.each(result.allGetCardsGroup, function(_cardsInfoTmp) {
                var pushCard = _.clone(_cardsInfoTmp);
                if (_.size(_cardsInfoTmp) > 3) {
                    pushCard = [_cardsInfoTmp[0], _cardsInfoTmp[1], _cardsInfoTmp[2]];
                }
                var otherCards = _.difference(_cardsInfo, pushCard);
                var pairs = this.getBeltPair(otherCards);
                pushCard.push(pairs[0]);
                pushCard.push(pairs[1]);
                resultMore.allGetCardsGroup.push(pushCard);
            }.bind(this));
        }
    }
    resultMore.isFind = _.size(resultMore.allGetCardsGroup) > 0;
    return resultMore;
};


/***AI出牌
 * 三张带对
 */
AIGetThree.doBeltPairs = function(_lastCardInfo, _cardsInfo) {
    var cardCnt = _.size(_cardsInfo);
    if (cardCnt < 5) {
        return {
            isFind: false
        };
    }
    var result = this.do(_lastCardInfo, _cardsInfo);
    /***找到可能三张*/
    if (result.isFind) {
        var recommendCards = result.recommendCards; ////推荐组这里是只有三张一样的牌
        var otherCards = _.difference(_cardsInfo, recommendCards);
        var pairs = this.getBeltPair(otherCards);
        if (_.size(pairs) > 0) {
            recommendCards.push(pairs);
            //计算带的那对子
            return {
                isFind: true,
                type: EumPkGroup.THREEONE,
                recommendCards: _.flatten(recommendCards), //推荐组
                allGetCardsGroup: result.allGetCardsGroup //所有组 
            };
        }
    }
    return {
        isFind: false
    };
};

/**只能计算带一张牌 */
AIGetThree.getBeltCard = function(cardsInfo) {
    // cardsInfo = _.sortBy(function(_cardInfo) {
    //     return -_cardInfo.value;
    // });
    var sameCards = FindSameCardValueTool.check(cardsInfo);
    //这里面做循环是没单->直接拆对子->拆三条->拆炸弹
    for (var i = 1; i <= 4; ++i) {
        var filterData = _.filter(sameCards, function(cards) {
            return i == _.size(cards);
        });
        if (_.size(filterData) > 0) {
            return filterData[0][0];
        }
    }
    return null;
};

/**只能计算带对 */
AIGetThree.getBeltPair = function(cardsInfo) {
    // cardsInfo = _.sortBy(function(_cardInfo) {
    //     return -_cardInfo.value;
    // });
    var sameCards = FindSameCardValueTool.check(cardsInfo);
    for (var i = 2; i <= 4; ++i) {
        var filterData = _.filter(sameCards, function(cards) {
            return i == _.size(cards);
        });
        if (_.size(filterData) > 0) {
            var tmpFind = filterData[0];
            return [tmpFind[0], tmpFind[1]];
        }
    }
};